package com.xindaoapp.happypet.adapter;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.bean.HomeData;
import com.xindaoapp.happypet.ui.PostDetailActivity;
import com.xindaoapp.happypet.utils.BitmapUtil;
import com.xindaoapp.happypet.utils.CommonParameter;
import com.xindaoapp.happypet.utils.CommonUtil;
import com.xindaoapp.happypet.utils.Constants;
import com.xindaoapp.happypet.view.CircleImageView;
import com.xindaoapp.happypet.view.RoundImageView;

public class HomeListAdapter extends BaseAdapter {
    private final Context context;
    private LinkedList<HomeData> mInfos;

    public HomeListAdapter(Context context, LinkedList<HomeData> mInfos) {
        super();
        this.context = context;
        this.mInfos = mInfos;
    }

    public LinkedList<HomeData> getmInfos() {
        return mInfos;
    }

    public void setmInfos(LinkedList<HomeData> mInfos) {
        this.mInfos = mInfos;
    }

    @SuppressWarnings("unused")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        final HomeData data = mInfos.get(position);
        if (convertView == null) {
            LayoutInflater layoutInflator = LayoutInflater.from(parent.getContext());
            convertView = layoutInflator.inflate(R.layout.activity_home_list_item, null);
            holder = new ViewHolder();
            holder.pet_icon = (RoundImageView) convertView.findViewById(R.id.pet_icon);
            holder.userIcon = (CircleImageView) convertView.findViewById(R.id.persion_icon);
            holder.title = (TextView) convertView.findViewById(R.id.tv_title);
            holder.userName = (TextView) convertView.findViewById(R.id.persion_name);
            holder.time = (TextView) convertView.findViewById(R.id.pet_time);
            holder.petName = (TextView) convertView.findViewById(R.id.pet_name);
            holder.lookTime = (TextView) convertView.findViewById(R.id.look_time);
            holder.careTime = (TextView) convertView.findViewById(R.id.care_time);
            holder.messageTime = (TextView) convertView.findViewById(R.id.message_time);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.pet_icon.mHeight = Integer.parseInt(data.getCoverheight());
        holder.pet_icon.mWidth = Integer.parseInt(data.getCoverwidth());

        if (CommonParameter.isLoadImg) {
            ImageLoader.getInstance().displayImage(data.getCoverpath(), holder.pet_icon);
            ImageLoader.getInstance().displayImage(data.getUserface(), holder.userIcon,
                    BitmapUtil.getPersonLoading());
        } else {
            holder.pet_icon.setImageResource(R.drawable.picture_loading);
            holder.userIcon.setImageResource(R.drawable.person_loading);
        }
        holder.title.setText(data.getSubject());
        holder.userName.setText(data.getAuthor());

        String time = CommonUtil.formatDate();
        if (data.getAddtime().split(" ")[0].equals(time.split(" ")[0])) {// 说明是今天
            String today = data.getAddtime().split(" ")[1];
            holder.time.setText(today.substring(0, 5));
        } else {
            holder.time.setText(data.getAddtime().split(" ")[0].substring(5, 10));
        }

        holder.petName.setText(data.getForumname());
        holder.lookTime.setText(data.getViews());
        holder.careTime.setText(data.getCover() + "");
        // holder.messageTime.setText(data.getReplies());

        convertView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PostDetailActivity.class);
                intent.putExtra(Constants.KEY_POST_TID, data.getTid());
                context.startActivity(intent);
                // 加入动画
                // overridePendingTransition(R.anim.new_dync_in_from_right,
                // R.anim.new_dync_out_to_left);
            }
        });

        return convertView;
    }

    class ViewHolder {
        RoundImageView pet_icon;// 宠物头像
        CircleImageView userIcon;// 用户头像
        TextView title;// 标题
        TextView userName;// 用户名字
        TextView time;// 时间
        TextView petName;// 宠物名字
        TextView lookTime;// 查看次数
        TextView careTime;// 关注次数
        TextView messageTime;// 留言次数
    }

    @Override
    public int getCount() {
        return mInfos.size();
    }

    @Override
    public Object getItem(int position) {
        return mInfos.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void addItemLast(List<HomeData> datas) {
        mInfos.addAll(datas);
    }

    public void clearItems() {
        if (null != mInfos) {
            mInfos.clear();
        }
    }

    public void addItemTop(List<HomeData> datas) {
        for (HomeData info : datas) {
            mInfos.addLast(info);
        }
    }
}
